<?php

namespace App\Admin\Controllers;

use App\Models\User;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\Tools\UserTools;
use Illuminate\Http\Request;

class MemberController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('会员管理');
            $content->description('');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('会员管理');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('会员管理');
            $content->description('');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {
            $grid->disableExport();
        	$grid->column('id','ID')->sortable();
            $grid->column('name','用户昵称');
            $grid->column('telephone','手机号码');

            $grid->created_at('注册时间');
            $grid->column('coin','金币');
            $grid->column('rechargerecords','充值记录')->display(function () {
            	return '<a href="' . admin_base_path('rechargerecords?user_id=' . $this->id ) . '" target="_blank">查看</a>';
            });
            
            $grid->column('downloadrecord','下载记录')->display(function () {
            	return '<a href="' . admin_base_path('downloadRecord?user_id=' . $this->id ) . '" target="_blank">查看</a>';
            });
            
            $grid->status('用户状态')->display(function($status){
            	return $status == 'enabled' ? '正常' : '禁用';
            });
            
            $grid->actions(function($actions){
            	$actions->disableDelete();
            	$actions->disableEdit();
            	
            	$row = $actions->row;
            	$to_action = $row['status'] == 'enabled' ? 'disabled' : 'enabled';
            	$actions->append(new UserTools($actions->getKey(), $row['status'], $to_action));
            });
            $grid->model()->orderBy('id','DESC');
            
            //查询
            $grid->filter(function ($filter) {
            	$filter->disableIdFilter();
            	
            	$filter->equal('telephone', '手机号码');
            	
            	$filter->equal('name', '用户名');
            	
            	$filter->in('status', '用户状态')->multipleSelect(['enabled' => '正常','disabled' => '禁用']);
            });
            
            $grid->disableCreation();//禁用创建
            $grid->disableExport();//禁用导出数据按钮
            $grid->disableRowSelector();//禁用行选择checkbox
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {
        	$states = [
        			'on'  => ['value' => 'enabled', 'text' => '正常', 'color' => 'success'],
        			'off' => ['value' => 'disabled', 'text' => '禁用', 'color' => 'danger'],
        	];
        	
            $form->display('id', 'ID');
            $form->text('name','用户昵称');
            $form->mobile('telephone','手机号码')->options(['mask' => '99999999999']);
            $form->password('password', '密码')->rules('required|confirmed');
            $form->password('password_confirmation', '确认密码')->rules('required')
            ->default(function ($form) {
            	return $form->model()->password;
            });
            	
            $form->ignore(['password_confirmation']);
            $form->switch('status','用户状态')->states($states);

            $form->display('created_at', '注册时间');
            
            $form->saving(function (Form $form) {
            	if ($form->password && $form->model()->password != $form->password) {
            		$form->password = bcrypt($form->password);
            	}
            });
        });
    }
    
    /**
     * @desc 操作禁用/启用 用户
     *
     * @return json
     */
    protected function userTools(Request $request)
    {
    	$user_id = $request->get('userid','');
    	$action = $request->get('action','');
    	
    	$actions = ['enabled', 'disabled'];
    	
    	if(!in_array($action, $actions) || empty($user_id))
    	{
    		return response()->json([
    				'message'=>'fail',
    				'status'=>0
    		]);
    	}
    	
    	User::where('id',$user_id)->update(['status'=>$action]);
    	
    	return response()->json([
    			'message'=>'success',
    			'status'=>1
    	]);
    }
}
